package com.ayuku;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ayuku.commons.Help;
import com.ayuku.commons.Util;
import com.ayuku.commons.business.Loginlog.LoginLogImpl;
import com.ayuku.commons.ui.LockDialog;

/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 *
 */
public class MainWindow extends org.eclipse.swt.widgets.Composite {
	private Menu mnuBar;
	private ToolItem tlbHelp;
	private ToolItem tlbLogOff;
	private ToolBar toolBar;
	private Composite clientArea;
	private Label txtStatus;
	private Composite statusArea;
	private ToolItem toolItem1;
	private ToolItem tlbExit;

	public MainWindow(final Composite parent, final int style) {
		super(parent, style);
		initGUI();
		clientArea.setFocus();
		Util.SHELL = getShell();
		Display.getDefault().asyncExec(new Runnable() {
			public void run() {
				initForm();
			}
		});
	}

	private void initForm() {
		try {
			// taruh initialisasi di sini

		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	/**
	 * 
	 * Initializes the GUI.
	 */
	private void initGUI() {
		final GridLayout thisLayout = new GridLayout();
		setLayout(thisLayout);

		GridData toolBarLData = new GridData();
		toolBarLData.grabExcessHorizontalSpace = true;
		toolBarLData.horizontalAlignment = GridData.FILL;
		toolBarLData.horizontalSpan = 3;
		toolBarLData.verticalSpan = 2;
		toolBarLData.heightHint = 50;
		toolBar = new ToolBar(this, SWT.FLAT);
		toolBar.setLayoutData(toolBarLData);
		toolBar.setBackgroundImage(SWTResourceManager.getImage(MainWindow.class, "/images/tb/bgtoolbar.png"));

		toolItem1 = new ToolItem(toolBar, SWT.NONE);
		toolItem1.setEnabled(false);
		toolItem1.setText("    ");

		tlbExit = new ToolItem(toolBar, SWT.NONE);
		tlbExit.setImage(SWTResourceManager.getImage(MainWindow.class, "/images/tb/door_out.png"));
		tlbExit.setToolTipText("Exit");
		tlbExit.setText("    E x i t    ");
		{
			tlbLogOff = new ToolItem(toolBar, SWT.NONE);
			tlbLogOff.setText("Lock App.");
			tlbLogOff.setImage(SWTResourceManager.getImage(MainWindow.class, "/images/tb/lock.png"));
			tlbLogOff.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					tlbLogOffWidgetSelected(evt);
				}
			});
		}

		tlbHelp = new ToolItem(toolBar, SWT.NONE);
		tlbHelp.setText("    Help    ");
		tlbHelp.setImage(SWTResourceManager.getImage(MainWindow.class, "/images/tb/help.png"));
		tlbHelp.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				tlbHelpWidgetSelected(evt);
			}
		});

		tlbExit.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent evt) {
				tblExitWidgetSelected();
			}
		});

		clientArea = new Composite(this, SWT.NONE);
		FormLayout clientAreaLayout = new FormLayout();

		GridData clientAreaLData = new GridData();
		clientAreaLData.grabExcessHorizontalSpace = true;
		clientAreaLData.grabExcessVerticalSpace = true;
		clientAreaLData.horizontalAlignment = GridData.FILL;
		clientAreaLData.verticalAlignment = GridData.FILL;
		clientArea.setLayoutData(clientAreaLData);
		clientArea.setLayout(clientAreaLayout);
		clientArea.setBackground(SWTResourceManager.getColor(0, 0, 0));
		// clientArea.setBackgroundImage(SWTResourceManager.getImage("images/app/office.jpg"));

		statusArea = new Composite(this, SWT.NONE);
		FormLayout statusAreaLayout = new FormLayout();

		statusArea.setLayout(statusAreaLayout);

		GridData statusAreaLData = new GridData();
		statusAreaLData.horizontalAlignment = GridData.FILL;
		statusAreaLData.grabExcessHorizontalSpace = true;
		statusAreaLData.heightHint = 24;
		statusArea.setLayoutData(statusAreaLData);
		statusArea.setBackground(SWTResourceManager.getColor(215, 213, 253));

		txtStatus = new Label(statusArea, SWT.NONE);
		txtStatus.setText("Ready");
		FormData txtStatusLData = new FormData();
		txtStatusLData.width = 328;
		txtStatusLData.height = 16;
		txtStatusLData.top = new FormAttachment(0, 1000, 5);
		txtStatusLData.left = new FormAttachment(0, 1000, 3);
		txtStatusLData.bottom = new FormAttachment(1000, 1000, -3);
		txtStatusLData.right = new FormAttachment(1000, 1000, -271);
		txtStatus.setLayoutData(txtStatusLData);
		txtStatus.setBackground(SWTResourceManager.getColor(215, 213, 253));

		thisLayout.verticalSpacing = 0;
		thisLayout.marginWidth = 0;
		thisLayout.marginHeight = 0;
		thisLayout.horizontalSpacing = 0;
		setSize(602, 412);

		this.addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent evt) {
				thisWidgetDisposed(evt);
			}
		});

		getShell().addListener (SWT.Close, new Listener () {
			public void handleEvent (Event event) {
				int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO;
				MessageBox messageBox = new MessageBox (getShell(), style);
				messageBox.setText ("Information");
				messageBox.setMessage ("Tutup aplikasi?");
				event.doit = messageBox.open () == SWT.YES;
			}
		});
		
		mnuBar = new Menu(getShell(), SWT.BAR);
		getShell().setMenuBar(mnuBar);

		getShell().setMaximized(true);

		try {
			Util.createMenu(mnuBar, Util.getCurrentUser().getUname());
		} catch (Exception e) {
			e.printStackTrace();
			Util.showMsgBox("Gagal create Menu, " + e.getMessage(), 2,
					getShell());
		}

	}

	public void doExitConfirm() {
		final MessageBox messageBox = new MessageBox(getShell(),
				SWT.ICON_WARNING | SWT.YES | SWT.NO);
		messageBox.setMessage("Do you really want to exit?");
		messageBox.setText("Exiting Application");

		final int response = messageBox.open();

		if (response == SWT.YES) {
			getShell().dispose();
			System.exit(0);

		} else {
			return;
		}

	}

	private void exitMenuItemWidgetSelected() {
		doExitConfirm();
	}

	private void tblExitWidgetSelected() {
		exitMenuItemWidgetSelected();
	}

	private void thisWidgetDisposed(DisposeEvent evt) {
		try {
			LoginLogImpl.add(Util.setLoginLog("logout"));
			System.out.println("update status:.........");
			System.exit(0);
		} catch (Exception e) {
			System.exit(0);
		}
	}

	private void tlbLogOffWidgetSelected(SelectionEvent evt) {
		doLogOfConfirm();
	}

	private LockDialog dlg = null;

	public void doLogOfConfirm() {
		/*
		 * final MessageBox messageBox = new MessageBox(getShell(),
		 * SWT.ICON_WARNING | SWT.YES | SWT.NO);
		 * messageBox.setMessage("Do you really want to log out?");
		 * messageBox.setText("Logout Application");
		 * 
		 * final int response = messageBox.open(); if (response == SWT.YES) {
		 */
		dlg = new LockDialog(getShell(), SWT.NONE);
		dlg.open();
		/*
		 * }
		 */
	}

	private void tlbHelpWidgetSelected(SelectionEvent evt) {
		Help hlp = new Help(getShell(), SWT.NONE);
		hlp.open();
	}

}
